<?php
define(SPORT_APPLY, "1");
define(TRACK_GROUP, "2");
define(FIELD_GROUP, "3");
define(RECORD_CARD, "4");
define(MAX_TRACK_ROAD, "6");
/**
 * showPrintOption()
 *
 * @param string $sTableId
 * @return
 */
function showPrintOption($sTableId = "")
{
    switch ($sTableId) {
        case SPORT_APPLY:
            return showSportApplyTable();
            break;
        case TRACK_GROUP:
            return showTrackGroupTable();
            break;
        case FIELD_GROUP:
            return showFieldGroupTable();
            break;
        case RECORD_CARD:
            return showRecordCardTable();
            break;
        default: ;
            break;
    }
}
/**
 * showSportApplyTable()
 *
 * @return
 */
function showSportApplyTable()
{
    $aMyClass = getCurrentClass();
    $form = new zeus_quickform(
        array("name" => "sv-form",
            "id" => "sv-form",
            "action" => "javascript:void(null);",
            "onsubmit" => "xajax_jsSubmitForm();",
            "method" => "post"));
    $html[] = "<table class='form-table'>";
    $html[] = "<tr class='form-field'>";
    $html[] = "<th scope='row'>";
    $html[] = "<label for='myclass_id'>";
    $html[] = _e('<strong>STEP 2:</strong>  Chose the print classes.');
    $html[] = "</label></th><td>";
    $html[] = "<tf class='submit'>";
    $html[] = $form->addElement("submit", array("name" => "step2", "id" => "step2", "value" => _e("Print Preview")));
    $html[] = "</tf>";
    $html[] = "</td></tr></table>";
    $html[] = "<table class='widefat'>";
    $html[] = "<tbody>";
    $html[] = printTheSelect("myclass[]", $aMyClass, $form);
    $html[] = "</tbody>";
    $html[] = "</table>";
    return implode("", $html);
}

/**
 * showTrackGroupTable()
 *
 * @return
 */
function showTrackGroupTable()
{
    $aTrack = getTrackGroup();
    $form = new zeus_quickform(
        array("name" => "sv-form",
            "id" => "sv-form",
            "action" => "javascript:void(null);",
            "onsubmit" => "xajax_jsSubmitForm();",
            "method" => "post"));
    $html[] = "<table class='form-table'>";
    $html[] = "<tr class='form-field'>";
    $html[] = "<th scope='row'>";
    $html[] = "<label for='myclass_id'>";
    $html[] = _e('<strong>STEP 2:</strong>  Chose the print group.');
    $html[] = "</label></th><td>";
    $html[] = "<tf class='submit'>";
    $html[] = $form->addElement("submit", array("name" => "step2", "id" => "step2", "value" => _e("Print Preview")));
    $html[] = "</tf>";
    $html[] = "</td></tr></table>";
    $html[] = "<table class='widefat'>";
    $html[] = "<tbody>";
    $html[] = printTheSelect("track[]", $aTrack, $form);
    $html[] = "</tbody>";
    $html[] = "</table>";
    return implode("", $html);
}

function showFieldGroupTable()
{
    $aField = getFieldGroup();
    $form = new zeus_quickform(
        array("name" => "sv-form",
            "id" => "sv-form",
            "action" => "javascript:void(null);",
            "onsubmit" => "xajax_jsSubmitForm();",
            "method" => "post"));
    $html[] = "<table class='form-table'>";
    $html[] = "<tr class='form-field'>";
    $html[] = "<th scope='row'>";
    $html[] = "<label for='myclass_id'>";
    $html[] = _e('<strong>STEP 2:</strong>  Chose the print group.');
    $html[] = "</label></th><td>";
    $html[] = "<tf class='submit'>";
    $html[] = $form->addElement("submit", array("name" => "step2", "id" => "step2", "value" => _e("Print Preview")));
    $html[] = "</tf>";
    $html[] = "</td></tr></table>";
    $html[] = "<table class='widefat'>";
    $html[] = "<tbody>";
    $html[] = printTheSelect("field[]", $aField, $form);
    $html[] = "</tbody>";
    $html[] = "</table>";
    return implode("", $html);
}

/**
 * showRecordCardTable()
 *
 * @return
 */
function showRecordCardTable()
{
    $form = new zeus_quickform(
        array("name" => "sv-form",
            "id" => "sv-form",
            "action" => "javascript:void(null);",
            "onsubmit" => "xajax_jsSubmitForm();",
            "method" => "post"));
    $html[] = "<table class='form-table'>";
    $html[] = "<tr class='form-field'>";
    $html[] = "<th scope='row'>";
    $html[] = "<label for='myclass_id'>";
    $html[] = _e('<strong>STEP 2:</strong>  Print the score record cards.');
    $html[] = "</label></th><td>";
    $html[] = "<tf class='submit'>";
    $html[] = $form->addElement("submit", array("name" => "step2", "id" => "step2", "value" => _e("Print Preview")));
    $html[] = "</tf>";
    $html[] = "</td></tr></table>";
    return implode("", $html);
}
/**
 * printTheSelect()
 *
 * @param array $aSelect
 * @return
 */
function printTheSelect($sName = "", $aSelect = array(), $oForm)
{
    if (isset($aSelect) && null != $aSelect) {
        $count = 0;
        $html[] = "<tr>";
        foreach($aSelect AS $key => $value) {
            $count++;
            $option = array("name" => $sName, "value" => $key);
            if (0 == $count % 5) {
                $html[] = "<td>" . $oForm->addElement("checkbox", $option) . $value ;
                $html[] = "</td></tr>";
            } else {
                $html[] = "<td>" . $oForm->addElement("checkbox", $option) . $value ;
                $html[] = "</td>";
            }
        }
    } else {
        zeus_do_nothing();
    }
    return implode("", $html);
}

/**
 * printSportApply()
 *
 * @return
 */
function printSportApply($aMyclassId = array())
{
    // Get sport name
    $sport_name = getLatestSportName();
    $myclass_name = getClassNames($aMyclassId);
    $project_name = getProjectName();
    $thead = array_merge(array(_e('Order'), _e('Name'), _e('Sex')), $project_name);
    // Get class name
    // Output
    $obj = new zeus_printer();
    $obj->setRowPerPage(30);
    $obj->setSign(_e('Class Master:'));
    $obj->setTHead($thead);
    $obj->printWordHead(_e('Print Report'));
    $obj->setTBStyle();
    // Output class name
    if (isset($aMyclassId) && null != $aMyclassId) {
        foreach($aMyclassId AS $myclass_id) {
            $applyData = getClassApplyResult($myclass_id);
            $obj->setCaption($myclass_name[$myclass_id]);
            $obj->write("<div id='content'>");
            $obj->write("<h4>" . $sport_name . _e('Apply Table') . "</h4>");
            $obj->tbStart();
            $obj->printTHead();
            $obj->printTBody($applyData);
            $obj->write("</div>");
        }
    } else {
        zeus_do_nothing();
    }
    $obj->output();
    $obj->__destruct();
}

/**
 * printTrackGroup()
 *
 * @param array $aGroupId
 * @return
 */
function printTrackGroup($aGroupId = array())
{
    $groupName = getGroupName($aGroupId);
    $obj = new zeus_printer();
    $obj->printWordHead(_e('Print Report'));
    $obj->setTBStyle("track-table");
    if (isset($aGroupId) && null != $aGroupId) {
        foreach($aGroupId AS $group_id) {
            $aGroupData = getGroupData($group_id);
            // var_dump($aGroupData);
            $obj->write($groupName[$group_id]);
            $teamCount = 0;
            if (isset($aGroupData) && null != $aGroupData) {
                foreach($aGroupData AS $aGroup) {
                    if (count($aGroup) < MAX_TRACK_ROAD) {
                        $aGroup = array_pad($aGroup, MAX_TRACK_ROAD, array('myclass_name' => "",
                                'road_id' => "",
                                'student_name' => ""));
                    } else {
                        zeus_do_nothing();
                    }
                    // var_dump($aGroup);
                    $obj->write("<br>" . (++$teamCount) . _e("Team:") . "<br>");
                    $obj->tbStart();
                    // Output row
                    for($rowIndex = 0;$rowIndex < count($aGroup);$rowIndex++) {
                        $obj->write("<tr>");
                        if (0 == $rowIndex) {
                            $obj->write("<td>");
                            $obj->write(_e("Class"));
                            $obj->write("</td>");
                            foreach($aGroup AS $data) {
                                $obj->write("<td>");
                                $obj->write($data['myclass_name']);
                                $obj->write("</td>");
                            }
                        } elseif (1 == $rowIndex) {
                            $obj->write("<td>");
                            $obj->write(_e("Road"));
                            $obj->write("</td>");
                            foreach($aGroup AS $data) {
                                $obj->write("<td>");
                                $obj->write($data['road_id']);
                                $obj->write("</td>");
                            }
                        } elseif (2 == $rowIndex) {
                            $obj->write("<td>");
                            $obj->write(_e("Name"));
                            $obj->write("</td>");
                            foreach($aGroup AS $data) {
                                $obj->write("<td>");
                                $obj->write($data['student_name']);
                                $obj->write("</td>");
                            }
                        } else {
                            zeus_do_nothing();
                        }
                        $obj->write("</tr>");
                    }
                    $obj->tbEnd();
                    $obj->write("<br>");
                }
            } else {
                zeus_do_nothing();
            }
        }
    } else {
        zeus_do_nothing();
    }
    $obj->output();
    $obj->__destruct();
}
/**
 * printFieldGroup()
 *
 * @param array $aGroupId
 * @return
 */
function printFieldGroup($aGroupId = array())
{
    $groupName = getGroupName($aGroupId);
    $obj = new zeus_printer();
    $obj->printWordHead(_e('Print Report'));
    $obj->setTBStyle("track-table");
    $obj->setTHead(array(_e('Order'), _e('Class'), _e('Name')));
    if (isset($aGroupId) && null != $aGroupId) {
        foreach($aGroupId AS $group_id) {
            $aGroupData = getGroupData($group_id);
            // var_dump($aGroupData);
            $obj->write($groupName[$group_id]);
            if (isset($aGroupData) && null != $aGroupData) {
                foreach($aGroupData AS $aGroup) {
                    $obj->tbStart();
                    $obj->printTHead();
                    if (isset($aGroup) && null != $aGroup) {
                        foreach($aGroup AS $data) {
                            $obj->write("<tr>");
                            $obj->write("<td>");
                            $obj->write($data['road_id']);
                            $obj->write("</td>");
                            $obj->write("<td>");
                            $obj->write($data['myclass_name']);
                            $obj->write("</td>");
                            $obj->write("<td>");
                            $obj->write($data['student_name']);
                            $obj->write("</td>");
                            $obj->write("</tr>");
                        }
                    } else {
                        zeus_do_nothing();
                    }
                    $obj->tbEnd();
                    $obj->write("<br>");
                }
            } else {
                zeus_do_nothing();
            }
        }
    } else {
        zeus_do_nothing();
    }
    $obj->output();
    $obj->__destruct();
}

function printRecordCard()
{
    $sport_id = getLatestSportId();
    $sql = "SELECT "
     . DB_PREFIX . "sport_group.group_name AS group_name,"
     . DB_PREFIX . "sport_group.group_id AS group_id,"
     . DB_PREFIX . "sport_arrangement.subteam_id AS subteam_id,"
     . DB_PREFIX . "sport_arrangement.road_id AS road_id,"
     . DB_PREFIX . "student.student_name AS student_name,"
     . DB_PREFIX . "rel_student_info.myclass_id AS myclass_id"
     . " FROM "
     . DB_PREFIX . "rel_student_info,"
     . DB_PREFIX . "sport_group,"
     . DB_PREFIX . "sport_arrangement,"
     . DB_PREFIX . "student"
     . " WHERE "
     . DB_PREFIX . "sport_group.group_id="
     . DB_PREFIX . "sport_arrangement.group_id"
     . " AND "
     . DB_PREFIX . "student.student_no="
     . DB_PREFIX . "sport_arrangement.student_no"
     . " AND "
     . DB_PREFIX . "rel_student_info.student_no="
     . DB_PREFIX . "sport_arrangement.student_no"
     . " AND "
     . DB_PREFIX . "sport_arrangement.sport_id='" . $sport_id . "'"
     . " ORDER BY "
     . DB_PREFIX . "sport_arrangement.group_id"
     . " ASC ";
    $result = zeus_sql_query($sql, false, 'All');
    $aMyclass = getCurrentClass();
    if (isset($result) && null != $result) {
        $obj = new zeus_printer();
        $obj->printWordHead(_e('Print Report'));
        $obj->setTBStyle("track-table");
        foreach($result AS $data) {
            $myclass_id = unserialize($data['myclass_id']);
            $myclass_name = $aMyclass[$myclass_id];
            $project = substr($data['group_id'], 3, 1);
            switch ($project) {
                case "A":
                case "B":
                case "C":
                case "D":
                case "E":
                case "F":
                    $obj->tbStart();
                    // First Row
                    $obj->write("<tr>");
                    $obj->write("<td>");
                    $obj->write(_e('Group') . _e(':') . $data['group_name']);
                    $obj->write("</td>");
                    $obj->write("<td>");
                    $obj->write(_e('Subteam') . _e(':') . $data['subteam_id']);
                    $obj->write("</td>");
                    $obj->write("<td>");
                    $obj->write(_e('Class') . _e(':') . $myclass_name);
                    $obj->write("</td>");
                    $obj->write("<td>");
                    $obj->write(_e('Name') . _e(':') . $data['student_name']);
                    $obj->write("</td>");
                    $obj->write("</tr>");
                    // Second Row
                    $obj->write("<tr>");
                    $obj->write("<td>");
                    $obj->write("</td>");
                    $obj->write("<td>");
                    $obj->write(_e('Road'));
                    $obj->write("</td>");
                    $obj->write("<td>");
                    $obj->write(_e('Score'));
                    $obj->write("</td>");
                    $obj->write("<td>");
                    $obj->write(_e('Rank'));
                    $obj->write("</td>");
                    $obj->write("</tr>");
                    switch ($project) {
                        case "A":
                        case "B":
                        case "C":
                            // Third Row
                            $obj->write("<tr>");
                            $obj->write("<td>");
                            $obj->write(_e('Tryout'));
                            $obj->write("</td>");
                            $obj->write("<td>");
                            $obj->write($data['road_id']);
                            $obj->write("</td>");
                            $obj->write("<td></td><td></td>");
                            $obj->write("</tr>");
                            // Fourth Row
                            $obj->write("<tr>");
                            $obj->write("<td>");
                            $obj->write(_e('Preliminary'));
                            $obj->write("</td>");
                            $obj->write("<td></td><td></td><td></td>");
                            $obj->write("</tr>");
                            // Fifth Row
                            $obj->write("<tr>");
                            $obj->write("<td>");
                            $obj->write(_e('Final'));
                            $obj->write("</td>");
                            $obj->write("<td></td><td></td><td></td>");
                            $obj->write("</tr>");
                            break;
                        case "D":
                        case "E":
                        case "F":
                            // Fifth Row
                            $obj->write("<tr>");
                            $obj->write("<td>");
                            $obj->write(_e('Final'));
                            $obj->write("</td>");
                            $obj->write("<td></td><td></td><td></td>");
                            $obj->write("</tr>");
                            break;
                        default:;
                            break;
                    }
                    $obj->tbEnd();
                    $obj->write("<br><br>");
                    break;
                default:
                    zeus_do_nothing();
                    break;
            }
        }
        $obj->output();
        $obj->__destruct();
    } else {
        return false;
    }
}

/**
 * getGroupData()
 *
 * @param string $sGroupId
 * @return
 */
function getGroupData($sGroupId = "")
{
    $termIndex = getTermIndex();
    $sql = "SELECT "
     . DB_PREFIX . "student.student_name AS student_name,"
     . DB_PREFIX . "rel_student_info.myclass_id AS myclass_id,"
     . DB_PREFIX . "sport_arrangement.subteam_id,"
     . DB_PREFIX . "sport_arrangement.road_id FROM "
     . DB_PREFIX . "sport_arrangement,"
     . DB_PREFIX . "rel_student_info,"
     . DB_PREFIX . "student WHERE "
     . DB_PREFIX . "sport_arrangement.group_id='" . $sGroupId
     . "' AND "
     . DB_PREFIX . "rel_student_info.student_no="
     . DB_PREFIX . "student.student_no"
     . " AND "
     . DB_PREFIX . "sport_arrangement.student_no="
     . DB_PREFIX . "student.student_no";
    $result = zeus_sql_query($sql, false, 'All');
    if (isset($result) && null != $result) {
        foreach($result AS $data) {
            $myclass_id = unserialize($data['myclass_id']);
            $aGroupData[$data['subteam_id']][$data['road_id']] = array('student_name' => $data['student_name'],
                'road_id' => $data['road_id'],
                'myclass_name' => getTheClassName($myclass_id)
                );
            ksort($aGroupData[$data['subteam_id']]);
        }
        return $aGroupData;
    } else {
        return false;
    }
}

/**
 * getGroupName()
 *
 * @param array $aGroupId
 * @return
 */
function getGroupName($aGroupId = array())
{
    if (isset($aGroupId) && null != $aGroupId) {
        foreach($aGroupId AS $group_id) {
            $sql = "SELECT group_name FROM " . DB_PREFIX . "sport_group WHERE group_id='" . $group_id . "'";
            $result = zeus_sql_query($sql, false);
            $groupName[$group_id] = $result['group_name'];
        }
        return $groupName;
    } else {
        return false;
    }
}
/**
 * getClassNames()
 *
 * @param array $aMyclassId
 * @return
 */
function getClassNames($aMyclassId = array())
{
    if (isset($aMyclassId) && null != $aMyclassId) {
        foreach($aMyclassId AS $myclass_id) {
            $sql = "SELECT myclass_name FROM " . DB_PREFIX . "myclass WHERE myclass_id ='" . $myclass_id . "'";
            $result = zeus_sql_query($sql, false);
            if (isset($result) && null != $result) {
                $className[$myclass_id] = $result['myclass_name'];
            } else {
                return false;
            }
        }
        return $className;
    } else {
        return false;
    }
}

/**
 * getProjectName()
 *
 * @return
 */
function getProjectName()
{
    $sql = "SELECT project_name FROM " . DB_PREFIX . "sport_project";
    $result = zeus_sql_query($sql, false, 'All');
    if (isset($result) && null != $result) {
        foreach($result AS $record) {
            $project_name[] = $record['project_name'];
        }
        return $project_name;
    } else {
        return false;
    }
}
/**
 * getClassApplyResult()
 *
 * @param string $sMyclassId
 * @return
 */
function getClassApplyResult($sMyclassId = "")
{
    $order = 1;
    $aStudentInfo = getTheClassStudentInfo($sMyclassId, 1);
    if (isset($aStudentInfo) && null != $aStudentInfo) {
        foreach($aStudentInfo AS $student) {
            $apply[] = array('order' => $order,
                'student_name' => $student['student_name'],
                'sex_id' => (1 == $student['sex_id'])?_e('Male'):_e('Female'),
                '100m' => getProjectStatus($student['projects'], 0),
                '200m' => getProjectStatus($student['projects'], 1),
                '400m' => getProjectStatus($student['projects'], 2),
                '800m' => getProjectStatus($student['projects'], 3),
                '1000m' => getProjectStatus($student['projects'], 4),
                '1500m' => getProjectStatus($student['projects'], 5),
                'high_jump' => getProjectStatus($student['projects'], 6),
                'long_jump' => getProjectStatus($student['projects'], 7),
                'throw' => getProjectStatus($student['projects'], 8),
                //'shot' => getProjectStatus($student['projects'], 9),
                );
            $order++;
        }
        return $apply;
    } else {
        return false;
    }
}
/**
 * getProjectStatus()
 *
 * @param string $sProjects
 * @param integer $iWhere
 * @return
 */
function getProjectStatus($sProjects = "", $iWhere = 0)
{
    $checked = "&radic;";
    $unchecked = "&nbsp;";
    switch ($iWhere) {
        case 0:
            return ("A" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 1:
            return ("B" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 2:
            return ("C" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 3:
            return ("D" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 4:
            return ("E" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 5:
            return ("F" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 6:
            return ("G" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 7:
            return ("H" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 8:
            return ("I" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        case 9:
            return ("J" == substr($sProjects, $iWhere, 1))?$checked:$unchecked;
            break;
        default:return $unchecked;
            break;
    }
}

?>